package xm.chengdu.com.beijing_211;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatCheckBox;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.czy1121.view.RoundButton;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import utils.FaSong;
import utils.JX_Utils;
import utils.SpfUtils;

public class LawnActivity extends Activity {

    @BindView(R.id.imageView2)
    ImageView imageView2;
    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.textView4)
    TextView textView4;
    @BindView(R.id.editftn)
    TextView editftn;

    @BindView(R.id.Email)
    TextView Email;
    @BindView(R.id.checkBox)
    AppCompatCheckBox checkBox;
    @BindView(R.id.editText2)
    EditText editText2;
    @BindView(R.id.Tel)
    TextView Tel;
    @BindView(R.id.ft)
    TextView ft;
    @BindView(R.id.checkBox2)
    AppCompatCheckBox checkBox2;
    @BindView(R.id.editft)
    EditText editft;
    @BindView(R.id.bt_clear)
    RoundButton btClear;
    int xuanzeS = 1;
    int xuanzeS2 = 1;
    @BindView(R.id.xxx)
    TextView xxx;
    @BindView(R.id.xxx2)
    TextView xxx2;
    @BindView(R.id.m)
    TextView m;
    @BindView(R.id.checkBox3)
    AppCompatCheckBox checkBox3;
    @BindView(R.id.editText3)
    EditText editText3;
    @BindView(R.id.checkBox4)
    AppCompatCheckBox checkBox4;
    @BindView(R.id.fts)
    TextView fts;
    @BindView(R.id.bt_clear2)
    RoundButton btClear2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lawn);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        //存储的围栏值

        String xuanze = (String) SpfUtils.get(LawnActivity.this, "xuanze", "1");
        String xuanze2 = (String) SpfUtils.get(LawnActivity.this, "xuanze2", "3");
        if (xuanze.equals("1")) {
            xuanzeS = 1;
            checkBox.setChecked(true);
            String xuanzezhi = (String) SpfUtils.get(LawnActivity.this, "xuanzezhi", "100");
            editText2.setText(xuanzezhi);
        } else {
            xuanzeS = 2;
            checkBox2.setChecked(true);
            String xuanzezhi = (String) SpfUtils.get(LawnActivity.this, "xuanzezhi2", "100");
            editft.setText(xuanzezhi);
        }
        if (xuanze2.equals("3")) {
            xuanzeS2 = 3;
            checkBox3.setChecked(true);

            String shezhiweilanzhim = (String) SpfUtils.get(LawnActivity.this, "shezhiweilanzhim", "");
            editText3.setText(shezhiweilanzhim);
        } else {
            xuanzeS2 = 4;
            checkBox4.setChecked(true);
             String shezhiweilanzhift = (String) SpfUtils.get(LawnActivity.this, "shezhiweilanzhift", "");

            editftn.setText(shezhiweilanzhift);
        }
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    checkBox2.setChecked(false);
                    xuanzeS = 1;
                }
            }
        });
        checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    checkBox.setChecked(false);
                    xuanzeS = 2;
                }
            }
        });
        checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    checkBox4.setChecked(false);
                    xuanzeS2 = 3;
                }
            }
        });
        checkBox4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    checkBox3.setChecked(false);
                    xuanzeS2 = 4;
                }
            }
        });
    }

    public void onEventMainThread(MyEvent event) {

        if (!TextUtils.isEmpty(event.getType())) {
            if (event.getType().equals("settingsuccess")) {
                finish();
            } else if (event.getType().equals("settingerr")) {
                finish();
            }


        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @OnClick({R.id.img_back, R.id.bt_clear, R.id.bt_clear2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.bt_clear2:



                if (xuanzeS2 == 3) {
                   String cishu = editText3.getText().toString();
                    if (TextUtils.isEmpty(cishu)) {
                        Toast.makeText(this, "Input can not be empty!", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    SpfUtils.put(LawnActivity.this, "xuanze2", "3");

                    //设置围栏
                    SpfUtils.put(LawnActivity.this, "shezhiweilanzhim", "" + cishu);
                    fasongweilan("" + cishu);
                } else {
                    String cishu = editftn.getText().toString();
                    if (TextUtils.isEmpty(cishu)) {
                        Toast.makeText(this, "Input can not be empty!", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    SpfUtils.put(LawnActivity.this, "xuanze2", "4");
                                       Float aFloat = Float.valueOf(editText3.getText().toString());
                    //米
                    double v = aFloat * 3.28;
                     Integer ints=(int)v;

                    //设置围栏
                    SpfUtils.put(LawnActivity.this, "shezhiweilanzhift", editftn.getText().toString());
                    fasongweilan("" +ints);
                }

                break;


            case R.id.bt_clear:
                String cishu = null;
                if (xuanzeS == 1) {
                    cishu = editText2.getText().toString();
                    if (TextUtils.isEmpty(cishu)) {
                        Toast.makeText(this, "Input can not be empty!", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    int i = Integer.parseInt(cishu);
                    int i1 = i / 100;
                    if (i1 == 0) {
                        cishu = "1";
                    } else {
                        if (i1 > 6) {
                            i1 = 6;
                            cishu = "6";
                        } else {
                            cishu = "" + i1;
                        }
                    }
                    SpfUtils.put(LawnActivity.this, "xuanze", "1");
                    SpfUtils.put(LawnActivity.this, "xuanzezhi", editText2.getText().toString());
                } else if (xuanzeS == 2) {
                    cishu = editft.getText().toString();
                    if (TextUtils.isEmpty(cishu)) {
                        Toast.makeText(this, "Input can not be empty!", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    int i = Integer.parseInt(cishu);
                    int i1 = (int) (i / 100 / 10.76);
                    if (i1 == 0) {
                        cishu = "1";
                    } else {
                        if (i1 > 6) {
                            i1 = 6;
                            cishu = "6";
                        } else {
                            cishu = "" + i1;
                        }
                    }
                    SpfUtils.put(LawnActivity.this, "xuanze", "2");
                    SpfUtils.put(LawnActivity.this, "xuanzezhi2", editft.getText().toString());
                }
                Log.e("222", "" + cishu);
                SpfUtils.put(LawnActivity.this, "cishu", cishu);
                Toast.makeText(this, "success!", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    public void fasongweilan(String s1) {

        int str3 = Integer.parseInt(s1);
        String s = JX_Utils.intToHexString(str3, 2);
        String oc = FaSong.oc(s);
        Log.e("888", "xx" + oc);
        MyEvent events4 = new MyEvent();
        events4.setType("message");
        events4.setContent(oc);
        EventBus.getDefault().post(events4);
    }

}
